package main

import (
	"fmt"
	"time"
)

const MAX_REQS = 5

var sem = make(chan int, MAX_REQS)	//定义一个大小为50的通道

type Req struct {
	a, b int
	//replyc chan int
}

func process(r *Req) {
	// todo
	fmt.Println("hello ................", r.a)
	time.Sleep(500 * time.Millisecond)
}

func handle(r *Req) {
	sem <- 1
	process(r)
	<- sem
}
func server2(service chan *Req) {
	for true {
		request := <- service
		go handle(request)
	}
}

func main() {
	service := make(chan *Req)
	go server2(service)

	for i := 0; i < 100; i++ {
		tmpReq := Req{i, i}
		service <- &tmpReq
	}
}
